﻿using System;
using Microsoft.Practices.Prism.Events;

namespace CPN.PJN.Silverlight.Common.Helpers
{
    public static class PrismExtensions
    {
        #region EventAggregator

        /// <summary>
        /// Simplifie la syntaxe de la méthode Publish de l'EventAggregator
        /// </summary>
        /// <typeparam name="TMessageType">CompositePresentationEvent</typeparam>
        /// <param name="aggregator">Event aggregator</param>
        /// <param name="message">Message</param>
        public static void Publish<TMessageType>(
            this IEventAggregator aggregator, TMessageType message)
              where TMessageType : CompositePresentationEvent<TMessageType>, new()
        {
            aggregator.GetEvent<TMessageType>().Publish(message);
        }

        /// <summary>
        /// Simplifie la syntaxe de la méthode Subscribe de l'EventAggregator
        /// </summary>
        /// <typeparam name="TMessageType">CompositePresentationEvent</typeparam>
        /// <param name="aggregator">Event aggregator</param>
        /// <param name="action">Callback</param>
        public static void Subscribe<TMessageType>(
          this IEventAggregator aggregator, Action<TMessageType> action)
            where TMessageType : CompositePresentationEvent<TMessageType>, new()
        {
            aggregator.GetEvent<TMessageType>().Subscribe(action);
        }

        /// <summary>
        /// Simplifie la syntaxe de la méthode Unsubscribe de l'EventAggregator
        /// </summary>
        /// <typeparam name="TMessageType">CompositePresentationEvent</typeparam>
        /// <param name="aggregator">Event aggregator</param>
        /// <param name="action">Callback</param>
        public static void Unsubscribe<TMessageType>(
          this IEventAggregator aggregator, Action<TMessageType> action)
            where TMessageType : CompositePresentationEvent<TMessageType>, new()
        {
            aggregator.GetEvent<TMessageType>().Unsubscribe(action);
        }

        #endregion
    }
}
